<template>
  <div style="width: 80%; margin-right: 10%; margin-left: 10%; margin-top: 2%">
    <el-form label-width="120px">
      <el-form-item label="标签名称">
        <el-input v-model="lable.lableName" placeholder="请填写标题" />
      </el-form-item>

      <el-form-item>
        <el-button
          v-if="isSave"
          type="primary"
          @click="saveOrUpdateLable"
          >保存</el-button
        >
        <el-button
          v-else
          type="primary"
          @click="saveOrUpdateLable"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
// <!-- :action="BASE_API+'/eduoss/fileoss/uploadFile'" -->
<script>
import label from "@/api/label";

export default {
  data() {
    return {
      isSave: true,
      lable: {
        isUse: 0,
      },
    }
  },

  watch: {
    $route(to, from) {
      this.init()
    },
  },

  mounted() {
  },

  created() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query && this.$route.query.id) {
        const lableId = this.$route.query.id
        this.isSave = true;
        label.getOneById(lableId).then( res => {
          if (res.success) {
            this.lable = res.data.lable
            console.log(this.lable)
          } else {
            this.$message({
              message: "系统异常,稍后再试",
              type: "failed",
            });
          }
        });
      } else {
        this.isSave = false;
      }
    },
    saveOrUpdateLable() {
      label.saveOrUpdate(this.lable).then(res => {
        if ( res.success ) {
            if ( this.isSave ) {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
            } else {
                this.lable = {}
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
            }
        } else {
            if ( this.isSave ) {
                this.$message({
                  message: "保存失败",
                  type: "error",
                });
            } else {
                this.lable = {}
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
            }
        }
      })
    },
  },
};
</script>

<!-- <style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 148px;
  display: block;
}
</style> -->